{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 注意！请根据提示操作！\n",
    "\n",
    "如果当页面加载完成，你还是看到了这条提示\n",
    "\n",
    "那么你的notebook并未被信任，导致代码无法正常运行\n",
    "\n",
    "点击右上角的`不可信`，将本notebook列入可信列表，即可正常运行。\n",
    "\n",
    "![可信按钮](./mgr/trust.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 【解答】红包分发\n",
    "\n",
    "请参考下列代码，理解后需自行修改，才能完成之前题目："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# \"\"\"三个引号会将内部的字符串连同换行符号在内全部原汁原味的保存下来，便于多行字符串的输入，省去每一行加一个\\n\n",
    "ipt = \"\"\"5\n",
    "dave\n",
    "laura\n",
    "owen\n",
    "vick\n",
    "amr\n",
    "dave\n",
    "200 3\n",
    "laura\n",
    "owen\n",
    "vick\n",
    "owen\n",
    "500 1\n",
    "dave\n",
    "amr\n",
    "150 2\n",
    "vick\n",
    "owen\n",
    "laura\n",
    "0 2\n",
    "amr\n",
    "vick\n",
    "vick\n",
    "0 0\n",
    "end\"\"\"\n",
    "\n",
    "content = ipt.split('\\n')[:-1]\n",
    "\n",
    "players = {}\n",
    "\n",
    "np = content[0].strip()\n",
    "\n",
    "for i in range(1, int(np) + 1):\n",
    "    players[content[i].strip()] = 0\n",
    "\n",
    "start = int(np) + 1\n",
    "\n",
    "while len(content) > start:\n",
    "    giver = content[start].strip()\n",
    "    amount, num = content[start + 1].strip().split(' ')\n",
    "    amount = int(amount)\n",
    "    num = int(num)\n",
    "    money = amount // num if num else 0  # if语句的省略用法，可以写在一行之内。\n",
    "    left = amount % num if num else amount\n",
    "    players[giver] -= amount\n",
    "    players[giver] += left\n",
    "    for i in range(num):\n",
    "        reciver = content[start + 2 + i].strip()\n",
    "        players[reciver] += money\n",
    "    start += 2 + num\n",
    "\n",
    "for k, v in players.items():\n",
    "    print(f'{k} {v}\\n')  # 此处输出的名字，顺序不一定会与上面一致，因为dict字典是非有序的，也就是说内部可能会乱序，需要自己想办法解决。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<hr>\n",
       "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
       "<script src=\"./mgr/submit.js\"></script>\n",
       "<script src=\"./mgr/hide.js\"></script>\n",
       "<div class=\"mdui-container-fluid\">\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
       "    </div>\n",
       "</div>\n",
       "<div id=\"html_output\">\n",
       "</div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<hr>\n",
    "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
    "<script src=\"./mgr/submit.js\"></script>\n",
    "<script src=\"./mgr/hide.js\"></script>\n",
    "<div class=\"mdui-container-fluid\">\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
    "    </div>\n",
    "</div>\n",
    "<div id=\"html_output\">\n",
    "</div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
